﻿using System;
using System.Runtime.Serialization;

namespace DataLayer.Types
{
    [Serializable]
    public class FrequencyItem : BaseRecord<FrequencyItem>, IEquatable<FrequencyItem>, ISerializable
    {

        private decimal mValuePerYear;
        private decimal valueInDays;

        public FrequencyItem()
        {
        }

        public virtual decimal ValuePerYear
        {
            get
            {
                return mValuePerYear;
            }
            set
            {
                mValuePerYear = value;
                OnPropertyChanged("ValuePerYear");
            }
        }

        public decimal ValueInDays 
        { 
            get
            {
                return valueInDays;   
            } 
            set
            {
                valueInDays = value;
                OnPropertyChanged("ValueInDays");
            }
        }

        public override string ToString()
        {
            return m_name;
        }

        #region IBaseRecord Members


        public virtual void Save()
        {
            base.Save(this);
//            if (m_id == 0)
//            {
//                StructureMap.ObjectFactory.GetInstance<IRepository<FrequencyItem>>().Add(this);
////                OnDataChanged(new NewObjectEventArgs(this));
//            }
//            else
//            {
//                StructureMap.ObjectFactory.GetInstance<IRepository<FrequencyItem>>().Update(this);
////                OnDataChanged(new ObjectUpdatedEventArgs(this));
//            }
        }

        public virtual void Delete()
        {
            base.Delete(this);
//            StructureMap.ObjectFactory.GetInstance<IRepository<FrequencyItem>>().Remove(this);
//            OnDataChanged(new ObjectDeletedEventArgs(this));
        }


        #endregion

        #region IEquatable<Frequency> Members

        public virtual bool Equals(FrequencyItem frequency)
        {
            return Id == frequency.Id;
        }

        #endregion

        public virtual void Merge(FrequencyItem frequencyItem)
        {
            m_name = frequencyItem.Name;
            mValuePerYear = frequencyItem.ValuePerYear;
        }


        #region ISerializable Members

        public FrequencyItem(SerializationInfo info, StreamingContext context)
        {
            m_name = info.GetString("Name");
            mValuePerYear = info.GetDecimal("ValuePerYear");
            ValueInDays = info.GetDecimal("ValueInDays");
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Name", Name);
            info.AddValue("ValuePerYear", ValuePerYear);
            info.AddValue("ValueInDays", ValueInDays);
        }

        #endregion
    }
}
